package cz.pproproj.aaservice.guimodule.model;

public class Credentials {

    private final boolean isTokenBased;
    private final String token;
    private final String username;
    private final String password;

    /**
     * Creates a new authorization based on token.
     *
     * @param token token to be set
     */
    public Credentials(String token) {
        this(token, null, null);
    }

    /**
     * Creates a new credentials based on username and password.
     *
     * @param username username to be set
     * @param password password to be set
     */
    public Credentials(String username, String password) {
        this(null, username, password);
    }

    private Credentials(String token, String username, String password) {
        this.isTokenBased = (token != null);
        this.token = token;
        this.username = username;
        this.password = password;
    }

    public boolean isTokenBased() {
        return isTokenBased;
    }


    public String getToken() throws UnsupportedOperationException {
        if (!isTokenBased) {
            throw new UnsupportedOperationException("It is not possible to retrieve token when username/password based authorization is used");
        }
        return token;
    }


    public String getUsername() throws UnsupportedOperationException {
        if (isTokenBased) {
            throw new UnsupportedOperationException("It is not possible to retrieve token when token based authorization is used");
        }
        return username;
    }


    public String getPassword() throws UnsupportedOperationException {
        if (isTokenBased) {
            throw new UnsupportedOperationException("It is not possible to retrieve token when token based authorization is used");
        }
        return password;
    }
}
